<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button
          v-if="permission.store_record_view"
          icon="el-icon-view"
          size="small"
          type="text"
          @click.stop="handleView(scope.row, scope.index)"
          >查 看</el-button
        >
      </template>
      <template slot="menuLeft">
        <!-- <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.store_delete"
          @click="handleDelete"
          >删 除
        </el-button> -->
      </template>
      <template slot="menuRight">
        <!-- <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          v-if="permission.store_import"
          @click="handleImport"
          >导入
        </el-button> -->
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.store_export"
          @click="handleExport"
          >导出
        </el-button>
      </template>
      <template slot-scope="{row}"
            slot="isConfirm">
        <el-tag type="success" :color="getColor(row.isConfirm)" effect="dark">{{ getIsOn(row.isConfirm) }}</el-tag>
      </template>
      <template slot-scope="{}" slot="serNoSearch">
        <select-page ref="searchSerSelect" label="serviceName" value="serviceNo" :select.sync="search.serNo" :url="website.searchLink.serviceUrl" placeholder-text="服务商" filter-key="searchParam"></select-page>
      </template>
      <template slot-scope="{}" slot="serCompanySearch">
        <select-page ref="searchOrgSelect" label="serviceName" value="serviceNo" :select.sync="search.serCompany" :url="website.searchLink.orgUrl" placeholder-text="所属机构" filter-key="searchParam"></select-page>
      </template>
    </avue-crud>
    <el-dialog
      title=""
      append-to-body
      :close-on-click-modal="false"
      :visible.sync="dialogTypeVisible"
      @close="dialogClose"
    >
      <template slot="" slot-scope="" v-if="dialogTypeVisible">
        <avue-crud
          :option="infoOption"
          :data="infoData"
          :search.sync="infoSearch"
          v-model="infoForm"
          :table-loading="infoloading"
          ref="infoCrud"
          :page.sync="pageInfo"
          @search-change="searchInfoChange"
          @refresh-change="refreshInfoChange"
          @on-load="onLoadInfo"
         >
        </avue-crud>
      </template>
    </el-dialog>
    <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
import {
  getList,
  getListSn,
  getExport,
  getExportTemplate,
} from "@/api/rb-oem/storeRecord/store_record";
import { mapGetters } from "vuex";
// import { getToken } from "@/util/auth";
import { downloadFileExcel } from "@/util/util";
import selectPage from "@/components/select-page/select-page";

var serviceDic = [{
  label:'包含下级',
  value: 1
}]
export default {
  components: {
    selectPage,
  },
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        searchMenuSpan: 6,
        columnBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            sortable: true,
            display: false,
            hide: true,
          },
          {
            label: "入库单号",
            prop: "rsrNo",
            type: "input",
            search: true,
            overHidden:true,
            rules: [
              {
                required: true,
                message: "请输入服务商编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "批次编号",
            prop: "batchCode",
            type: "input",
            search: true,
            minWidth: 100,
            overHidden:true,
          },
          {
            label: "服务商",
            prop: "serNo",
            type: "select",
            showColumn: false,
            search: true,
            searchOrder: 1,
            filterable: true,
            hide: true,
            rules: [
              {
                required: true,
                message: "请选择服务商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "服务商编号",
            prop: "serviceNo",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入服务商编号",
                trigger: "blur",
              },
            ],
            minWidth: 110,
            overHidden:true,
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            type: "input",
            display: false,
            rules: [
              {
                required: true,
                message: "请输入服务商名称",
                trigger: "blur",
              },
            ],
            minWidth: 180,
            overHidden:true,
          },
          {
            label: '',
            prop: 'isAllSer',
            type: 'select',
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            search: true,
            searchOrder: 2,
            searchType:"checkbox",
            searchSpan: 0,
            searchLabelWidth: '0',
            dicData: serviceDic
          },
          {
            label: "所属机构",
            prop: "serCompany",
            type: "select",
            showColumn: false,
            search: true,
            searchOrder: 3,
            filterable: true,
            hide: true,
            rules: [
              {
                required: true,
                message: "请选择服务商",
                trigger: "blur",
              },
            ],
          },
          {
            label: "操作人编号",
            prop: "createUserNo",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入操作人编号",
                trigger: "blur",
              },
            ],
            minWidth: 100,
            overHidden:true,
          },
          {
            label: "操作人",
            prop: "createUserName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入操作人",
                trigger: "blur",
              },
            ],
            minWidth: 180,
            overHidden:true,
          },
          {
            label: "入库数量",
            prop: "amount",
            type: "input",
            overHidden:true,
          },
          {
            label: "签收状态",
            prop: "isBind",
            type: "select",
            search: true,
            hide: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=is_confirm",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            rules: [
              {
                required: true,
                message: "请选择设备状态",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "是否签收",
          //   prop: "isConfirm",
          //   type: "input",
          //   width: 80
          // },
          {
            label: "创建日期",
            prop: "createTime",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            overHidden:true,
            minWidth:140
          },
        ],
      },
      infoloading: true,
      infoSearch: {},
      infoRsrNo: '',
      infoForm: {},
      infoData: [],
      infoOption: {
        height: "auto",
        calcHeight: '100%',
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        menu: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        searchMenuSpan: 6,
        columnBtn: false,
        rowKey:'id',
        rowParentKey:'parentId',
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            sortable: true,
            display: false,
            hide: true,
          },
          {
            label: "入库单号",
            prop: "rsrNo",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入服务商编号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "设备类型",
            prop: "rptName",
            type: "input",
            width: 100,
            rules: [
              {
                required: true,
                message: "请输入设备类型",
                trigger: "blur",
              },
            ],
          },
          {
            label: "设备品牌",
            prop: "rpsName",
            type: "input",
            width: 100,
            rules: [
              {
                required: true,
                message: "请输入设备品牌",
                trigger: "blur",
              },
            ],
            overHidden:true
          },
          {
            label: "设备型号",
            prop: "rpmName",
            type: "input",
            rules: [
              {
                required: true,
                message: "请输入设备型号",
                trigger: "blur",
              },
            ],
            minWidth: 180,
            overHidden:true
          },
          {
            label: "设备型号",
            prop: "rpmNo",
            type: "select",
            search: true,
            dicUrl: "/api/rb-oem/oem/store/store/selectModelList?current=-1&size=-1",
            props: {
              label: "rpmName",
              value: "rpmNo",
              desc: "rpmNo"
            },
            hide: true,
            rules: [
              {
                required: true,
                message: "请选择设备型号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "设备编号",
            prop: "rsSn",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入设备编号",
                trigger: "blur",
              },
            ],
            minWidth: 180,
            overHidden:true
          },
          {
            label: "创建日期",
            prop: "createTime",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            width: 140,
            overHidden:true
          },
        ],
      },
      data: [],
      dialogTypeVisible: false,
      pageInfo: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-oem/oem/store/store/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.store_add, false),
        viewBtn: this.vaildData(this.permission.store_view, false),
        delBtn: this.vaildData(this.permission.store_delete, false),
        editBtn: this.vaildData(this.permission.store_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
    getIsOn() {
      return state => {
        if (state == '0') {
          return '未确认';
        } else if (state == '1') {
          return '已确认';
        } else {
          return '';
        }
      }
    },
    getColor() {
      return state => {
        if (state == '0') {
          return '#F56C6C';
        } else if (state == '1') {
          return '#67C23A';
        }
      }
    },
  },
  methods: {
    /**
     * @func 入库记录子集
     * @desc 查看该条子集
     * @param {Object} row - 仓库信息
     * */
    handleView(row) {
      this.dialogTypeVisible = true;
      this.pageInfo.currentPage = 1;
      this.infoRsrNo = row.rsrNo;
    },
    /* listSn 搜索 */
    searchInfoChange(params, done) {
      this.infoSearch = params;
      this.pageInfo.currentPage = 1;
      this.onLoadInfo(this.pageInfo, params);
      done();
    },
    /* 驳回类型 刷新 */
    refreshInfoChange() {
      this.onLoadInfo(this.pageInfo, this.infoSearch);
    },
    /* listSn 分页 */
    onLoadInfo(page, params = {}) {
      this.infoloading = true;
      getListSn(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.infoSearch, {rsrNo: this.infoRsrNo})
      ).then((res) => {
        const data = res.data.data;
        this.pageInfo.total = data.total;
        this.infoData = data.records;
        this.infoloading = false;
      });
    },
    /* 关闭dialog */
    dialogClose() {
      this.infoSearch = {}
    },
    // rowSave(row, done, loading) {
    //   add(row).then(
    //     () => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //       done();
    //     },
    //     (error) => {
    //       loading();
    //       window.console.log(error);
    //     }
    //   );
    // },
    /* rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    }, */
    // rowDel(row) {
    //   this.$confirm("确定将选择数据删除?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       return remove(row.id);
    //     })
    //     .then(() => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //     });
    // },
    // handleDelete() {
    //   if (this.selectionList.length === 0) {
    //     this.$message.warning("请选择至少一条数据");
    //     return;
    //   }
    //   this.$confirm("确定将选择数据删除?", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning",
    //   })
    //     .then(() => {
    //       return remove(this.ids);
    //     })
    //     .then(() => {
    //       this.onLoad(this.page);
    //       this.$message({
    //         type: "success",
    //         message: "操作成功!",
    //       });
    //       this.$refs.crud.toggleSelection();
    //     });
    // },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleImport() {
      this.excelBox = true;
    },
    handleExport() {
      // let exQuery = ``;
      // exQuery += `&rptNo=${this.search.rptNo}`;
      // exQuery += `&rpsNo=${this.search.rpsNo}`;
      // exQuery += `&rpmNo=${this.search.rpmNo}`;
      // exQuery += `&productType=${this.search.productType}`;
      // exQuery += `&rsSn=${this.search.rsSn}`;
      // exQuery += `&rpiNo=${this.search.rpiNo}`;
      // exQuery += `&rsServiceNo=${this.search.rsServiceNo}`;
      // exQuery += `&isBind=${this.search.isBind}`;
      // exQuery += `&rsBindTime=${this.search.rsBindTime}`;
      // this.$confirm("是否导出用户数据?", "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   type: "warning",
      // }).then(() => {
      //   window.open(
      //     `/api/rb-oem/oem/store/store/export?${
      //       this.website.tokenHeader
      //     }=${getToken()}` + exQuery
      //   );
      // });

      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        getExport(this.search).then((res) => {
            downloadFileExcel(res);
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    handleTemplate() {
      // window.open(
      //   `/api/rb-oem/oem/store/store/export-template?${
      //     this.website.tokenHeader
      //   }=${getToken()}`
      // );
      getExportTemplate().then((res) => {
          downloadFileExcel(res);
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/ .el-tag--dark.el-tag--success {
  border-color: transparent;
  opacity: 0.8;
}
</style>